package com.game.gameHandle
{
	import com.game.gameObject.*;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.geom.*;
	import flash.net.URLRequest;
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.*;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.utils.ObjectProxy;
	
	public class GameScene
	{
		protected static var instance:GameScene = null;
		
		protected var xmlRequest:HTTPService;
		
		public function GameScene()
		{
		}

		static public function get Instance():GameScene
		{
			if ( instance == null )
				instance = new GameScene();
			return instance;
		}
		
		public function startup():void
		{
			trace("开始配置场景...");

			
			this.xmlRequest = new HTTPService();
			this.xmlRequest.url = "./com/game/map/house.tmx";
			this.xmlRequest.addEventListener("result", xmlLoaded);
			this.xmlRequest.addEventListener("fault", xmlFailed);
			this.xmlRequest.send();
			Application.application.loader.setProgress(5,100);
			Application.application.loader.label = "载入地图..." + " 5%";
			
		}
		public function xmlLoaded(event:ResultEvent):void
		{
			Application.application.loader.setProgress(10, 100);
			Application.application.loader.label = "开始创建地图..." + " 10%";
			
			var layers:ArrayCollection =  new ArrayCollection();
			var tileSets:ArrayCollection = new ArrayCollection();
			if (event.result.map.layer is ArrayCollection) {
				layers = event.result.map.layer;
			}
			else {
				layers.addItem(event.result.map.layer);
			}
			if (event.result.map.tileset is ArrayCollection) {
				tileSets = event.result.map.tileset;
			}
			else {
				tileSets.addItem(event.result.map.tileset);
			}
			
			[Embed(source="../media/TileA4.png")]
			var wall:Class;
			[Embed(source="../media/TileC.png")]
			var innerObj:Class;
			[Embed(source="../media/TileA5.png")]
			var board:Class;
		
			var columns:Number = event.result.map.width as Number;
			var rows:Number = event.result.map.height as Number;
			var sceneBackGround:TiledBackground = new TiledBackground();
			sceneBackGround.tileWidth = event.result.map.tilewidth;
			sceneBackGround.tileHeight = event.result.map.tileheight;
			
			//循环每一层
			for (var l:int = 0; l < layers.length; l++) {
				var mapArr:ArrayCollection = layers[l].data.tile;
				sceneBackGround.tiles[l] = new Array();
				for (var i:int = 0, j:int = 0, k:int = 0; k < mapArr.length;) {
					if (!(sceneBackGround.tiles[l][i] is Array))
						sceneBackGround.tiles[l][i] = new Array();
					for (var s:int = 0; s < tileSets.length; s++) {
						if (((s + 1) == tileSets.length) || mapArr[k].gid < (tileSets[s + 1].firstgid as Number)) {
							var tileSet:ObjectProxy = tileSets[s];
							break;
						}
					}
					var resource:DisplayObject;
					switch (tileSet.name) {
						case "wall":
							if (!(resource is wall))
								resource = new wall();
							break;
						case "innerObj":
							if (!(resource is innerObj))
								resource = new innerObj();
							break;
						case "board":
							if (!(resource is board))
								resource = new board();
							break;
						default:
					}
					mapArr[k].gid -= tileSet.firstgid;
					
					sceneBackGround.tiles[l][i][j] = new GraphicsResource(resource, 1, 1, new Rectangle(mapArr[k].gid%(resource.width/tileSet.tilewidth)*tileSet.tilewidth, Math.floor(mapArr[k].gid/(resource.width/tileSet.tilewidth))*tileSet.tileheight, tileSet.tilewidth, tileSet.tileheight));
					j++;
					k++;
					if (j == columns) {
						j = 0;
						i++;
					}
				}
			}
			sceneBackGround.startupTiledBackground();
			
			Application.application.loader.setProgress(80, 100);
			Application.application.loader.label = "开始创建人物..." + " 80%";
			//加入人物
			GameObjectManager.Instance.curUser = new Player('dalton');
			GameObjectManager.Instance.curUser.point = new Point(0, 0);
			GameObjectManager.Instance.curUser.startupPlayer();
			
			
			Application.application.loader.visible = false;

		} 
		public function xmlFailed(event:FaultEvent):void
		{
			  Alert.show(event.fault.faultString);
			  
		} 
			
	}
}